package com.mycat.manager.controller.ucenter;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.mycat.common.entity.InvokeResult;
import com.mycat.dao.dto.common.CommonDTO;
import com.mycat.dao.entity.ucenter.Customer;
import com.mycat.service.ucenter.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/customer")
public class CustomerController {

	private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	private CustomerService customerService;

	/**
	 * 页面跳转
	 */
	@RequestMapping(method = RequestMethod.GET)
	@RequiresPermissions("customer:view")
	public String toList() {
		return "ucenter/Customer";
	}

	/**
	 * 获取分页数据
	 *
	 * @param pageNum  页码
	 * @param pageSize 页大小
	 */
	@RequestMapping(value = "/{pageNum}/{pageSize}", method = RequestMethod.GET)
	@RequiresPermissions("customer:view")
	@ResponseBody
	public ResponseEntity<InvokeResult<PageInfo<Customer>>> page(@PathVariable int pageNum, @PathVariable int pageSize) {
		InvokeResult invokeResult = null;
		try {
			Page<Customer> page = new Page<Customer>() {{
				this.setPageNum(pageNum);
				this.setPageSize(pageSize);
			}};
			PageInfo<Customer> pageInfo = customerService.page(page);
			if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
				invokeResult = InvokeResult.success(pageInfo);
			} else {
				invokeResult = InvokeResult.pass_no_data();
			}
		} catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return new ResponseEntity<>(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
	}

	/**
	 * 获取单个对象
	 *
	 * @param id Id
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@RequiresPermissions("customer:view")
	@ResponseBody
	public ResponseEntity<InvokeResult<Customer>> getCustomer(@PathVariable Integer id) {
		InvokeResult invokeResult = null;
		try {
			if (null != id) {
				Customer customer = customerService.findCustomer(id);
				invokeResult = InvokeResult.success(customer);
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

	/**
	 * 删除对象
	 *
	 * @param commonDTO 删除数据DTO
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequiresPermissions("customer:delete")
	@ResponseBody
	public ResponseEntity<InvokeResult> delete(@RequestBody CommonDTO commonDTO) {
		InvokeResult invokeResult = null;
		try {
			if (null != commonDTO && null != commonDTO.getIds() && commonDTO.getIds().length > 0) {
				customerService.delete(commonDTO.getIds());
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

    /**
     * 新增数据
     *
     * @param customer 新增对象
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("customer:add")
    @ResponseBody
    public ResponseEntity<InvokeResult> add(Customer customer) {
        InvokeResult invokeResult = null;
        try {
			if (null != customer) {
				customerService.save(customer);
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
        } catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
        }
        return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

	/**
	 * 修改数据
	 *
	 * @param customer 修改对象
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@RequiresPermissions("customer:edit")
	@ResponseBody
	public ResponseEntity<InvokeResult> edit(Customer customer) {
		InvokeResult invokeResult = null;
		try {
			if (null != customer && null != customer.getId()) {
				customerService.edit(customer);
				invokeResult = InvokeResult.success();
			} else {
				invokeResult = InvokeResult.failure("参数错误！");
			}
		} catch (Exception e) {
			invokeResult = InvokeResult.error();
			logger.error(e.getMessage(), e);
		}
		return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
	}

}